package com.joycessh.controller;

import com.joycessh.domain.Result;
import com.joycessh.domain.ResultCode;
import com.joycessh.domain.ResultJson;
import com.joycessh.search.index.config.HbaseTest;
import com.joycessh.search.index.entity.Page;
import com.joycessh.search.index.entity.PageInfo;
import com.joycessh.search.index.service.IndexService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Joycessh
 * @Description
 * @Date 4/27/19 7:27 PM
 */
@Api(description = "搜索相关API")
@RestController
@RequestMapping("/api/v1/index")
public class SearchController {

    @Autowired
    private HbaseTest hTest;

    @Autowired
    private IndexService indexService;

    @PostMapping(value = "/test")
    public String run() throws IOException {
        hTest.run();
        System.out.println("hbase test");
        return "ff";
    }

    /**
     * 搜索
     * @author Joycessh
     * @param pageNum
	 * @param pageSize
	 * @param value
     * @return java.util.List<com.joycessh.search.index.entity.Page>
     * @date 5/4/19 12:29 PM
     */
    @ApiOperation(value = "搜索", notes = "根据用户输入的关键字检索内容")
    @PostMapping(value = "/{pageNum}/{pageSize}/{value}")
    public Result search(@PathVariable int pageNum,
                         @PathVariable int pageSize,
                         @PathVariable String value) throws ParseException, InvalidTokenOffsetsException, IOException {

        PageInfo<Page> pageInfo = indexService.search(pageNum, pageSize, value);
        if (pageInfo == null) {
            return Result.failure(ResultCode.SEARCH_NOT_FOUND);
        }
        return Result.success().addData("pageInfo", pageInfo);
    }
}
